{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "9ae3bc2d-8628-45b5-9925-b7e6ceb627c4",
   "metadata": {},
   "source": [
    "# 第三节课读书笔记"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d2c7440f-5413-4242-9905-9ad234ce3659",
   "metadata": {
    "jp-MarkdownHeadingCollapsed": true
   },
   "source": [
    "## Python中取变量的名字\n",
    "\n",
    "Python中变量的名字:字母+数字+\"_\"三类组成；首字不能是数字；不能与\"保留字\"相同。\n",
    "\n",
    "未来变量命名，有以下要求：\n",
    "\n",
    "- 不用无意义的变量名称\n",
    "- 尽可能用英文表示变量含义\n",
    "- 如果有多个单词，用下划线连接\n",
    "\n",
    "\n",
    "- 程序代码示例：\n",
    "\n",
    "         i=1\n",
    "          print(i)\n",
    "  ***\n",
    "  "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0ba49c2b-3aaa-4988-af77-305d16772b58",
   "metadata": {},
   "source": [
    "## Python变量的类型（Variable Type）\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e767776b-6826-4c7b-9aac-7ed2139a0d27",
   "metadata": {},
   "source": [
    "Python变量类型有三类：\n",
    "\n",
    "- 数值型：int（整数），float（浮点数），complex（复数），bool（布尔型：Ture or False）\n",
    "\n",
    "- 字符型：str（字符串）\n",
    "\n",
    "- 空：None\n",
    "\n",
    "class:类"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "f6cbdb02-37ca-4767-ab70-6a9ff563cb68",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'float'>\n"
     ]
    }
   ],
   "source": [
    "x=1.0\n",
    "print(type(x))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "ab7b8f1e-9ee5-4fb2-8f5a-60a0d2706df2",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "isinstance(x,float)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5c7ac4dc-4ccc-4fa1-b1d5-f7250a76000e",
   "metadata": {},
   "source": [
    "isinstance() 是 Python 中的一个内建函数，用于检查一个对象是否是一个已知的类型。这个函数通常用于类型检查，以确保在代码中可以安全地使用特定的操作或属性。\n",
    "- isinstance(object, classinfo)\n",
    "- object 是要检查的对象。\n",
    "- classinfo 是要检查的类或类的元组。\n",
    "如果 object 是 classinfo 的实例，或者 classinfo 包含了 object 的类，那么 isinstance() 函数将返回 True，否则返回 False。"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "deb7f40c-6e34-4f3b-b7fb-4b7e943661e9",
   "metadata": {},
   "source": [
    "## Python的运算符\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a602721a-ea1d-4573-aae7-b487a7d2b6ac",
   "metadata": {},
   "source": [
    "- 数值计算：+ - * /  **(次方)  %（取余） //（整数除，向下取整） +=  -=  *=  /=  == > < >= <= & | ^ ! ()\n",
    "- 字符串运算符：+，*"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "8be3f3d5-08ed-4fea-b8fa-3daed3a6e01c",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "4\n"
     ]
    }
   ],
   "source": [
    "a=6&4\n",
    "print(a)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b7653263-ef79-4fb7-9e18-fe243c89cfe0",
   "metadata": {},
   "source": [
    "注：&:二进制\"与\"\n",
    " 将十进制数转换为二进制数通常使用“除以2取余法”\r",
    "- \r\n",
    "将十进制数除以2，记录余数- 。\r\n",
    "将商继续除以2，重复记录余数，直到商为- 0。\r\n",
    "将记录的余数逆序排列，即为二进制数"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "106c4181-5b3b-4db2-8374-88d726ab348e",
   "metadata": {},
   "source": [
    "## Python变量的扩展\n",
    "\n",
    "bool -> int -> float -> complex"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "a871b9f6-2e63-4b98-9707-e3af8ef434b4",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "int"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a= True\n",
    "b=2\n",
    "a+b\n",
    "c=a+b\n",
    "type(c)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "7cf14f8e-28bd-498c-b428-125e412f16c9",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "1==True\n",
    "0==False"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "95e94654-1596-452a-98b4-13c9b2dc5342",
   "metadata": {},
   "source": [
    "## Python字符串函数与切片\n",
    "\n",
    "10\t \t*\t00 22 37.0\t2\t+63 52.0\t2\t110.0\t17.0\t \t5.4\t00 25 25.1\t+64 08 36"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "0487f71c-35d7-41d1-ab6c-bf7628055b10",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "20"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a= \"Guangzhou University\"\n",
    "len(a)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "01287a74-0cbf-46c4-b8df-bd09d414653e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "\"zhou\" in a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "edb2d7b0-71a7-4043-990a-cff87e1a339d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'zh'"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a=\"Guangzhou\"\n",
    "a[5:7]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "9bb53a0c-d298-4a3e-a4ee-62b9109fcef9",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'22 37.0'"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "c=\"10\t \t*\t00 22 37.0\t2\t+63 52.0\t2\t110.0\t17.0\t \t5.4\t00 25 25.1\t+64 08 36\"\n",
    "c[10:17]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "b5191fb6-dee9-4223-89d7-b9b918af2e77",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0.3769444444444444\n"
     ]
    }
   ],
   "source": [
    "deg=c[7:9]\n",
    "minute=c[10:12]\n",
    "sec=c[13:17]\n",
    "print(eval(deg)+ eval(minute)/60 +eval(sec)/3600.)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "efe92db2-ec52-4d35-8064-f80faa66e7fb",
   "metadata": {},
   "source": [
    "##  Python的格式输出\n",
    "\n",
    "\n",
    "- %\n",
    "- format\n",
    "- fString\n",
    "\n",
    "\"格式说明\" %（要显示输出的变量）\n",
    "   %d %f %s "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "93fb7e18-f075-45bf-a9c7-0fc876c14744",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "3.14159265358979\n"
     ]
    }
   ],
   "source": [
    "pi=3.14159265358979\n",
    "print(pi)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "965cc6fb-0f85-4bde-bfaa-f8951e7a9e80",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "   3   4\n",
      "003.1416\n"
     ]
    }
   ],
   "source": [
    "a=3\n",
    "b=4\n",
    "c=100\n",
    "print('%4d%4d' % (a,b))\n",
    "print(\"%08.4f\" % pi)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "id": "85af96c7-3505-4aed-90ef-aea0af327279",
   "metadata": {},
   "outputs": [],
   "source": [
    "import random\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "id": "e94dd674-c1b2-43cd-afaf-c59372266029",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0.9654895507322414\n"
     ]
    }
   ],
   "source": [
    "print(random.random())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "id": "6c3be6ff-d0be-44a3-ac0d-c60d5f8d8128",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "  41.498   3.999  89.908   3.253  28.989  20.815  52.308  87.206  55.292  93.804\n",
      "  51.230  83.425  86.156   1.692  45.319   1.462   7.940  97.248  32.067  63.868\n",
      "  20.623  21.735  75.924  85.671  14.750  75.000  82.851  57.770  20.403  40.937\n",
      "  19.466  44.287  64.048  13.979  73.555  53.213  72.574  45.515  31.117  49.902\n",
      "  62.156  98.672  83.786  57.778  60.162  48.607  15.137  98.908  53.155  53.622\n",
      "  89.076  67.726  29.388   8.764  72.021   4.337  34.605  98.684  80.718  40.092\n",
      "  72.210  81.357  56.901  27.972  99.428  95.808  32.115  97.151   5.697   9.988\n",
      "  88.391  90.613  12.377  54.239  58.223  14.404  92.367  72.583  24.850  56.641\n",
      "  54.243   2.873  40.651  66.446  57.948  71.741  35.909  16.814   2.432  23.335\n",
      "   9.596  35.319  19.073  21.689  12.469  94.787  30.594   4.017   8.221  74.046\n"
     ]
    }
   ],
   "source": [
    "for i in range(0,100):\n",
    "    a=random.uniform(0,100)\n",
    "    print(\"%8.3f\" % a,end=\"\")\n",
    "    if ((i+1)%10==0):\n",
    "        print()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d3f1abd9-fafb-4fec-ba93-b34435f2f76d",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
